package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetFindEmailRandModel;

public class FindEmailPasswordActivity extends BaseActivity implements View.OnClickListener {
    ImageView findemail_fh;
    EditText findemail_email,findemail_rand;
    TextView findemail_hqyzm;
    Button findemail_btn;
    private int i=60;
    private Dialog mDialog;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0) {
                findemail_hqyzm.setText("点击重新获取");
                findemail_hqyzm.setClickable(true);
                i = 60;
            } else {
                findemail_hqyzm.setText(msg.arg1 + "秒后可重新发送");
                startTime();
                findemail_hqyzm.setClickable(false);
            }
        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_find_email_password);
        findemail_fh =(ImageView)findViewById(R.id.findemail_fh);
        findemail_email=(EditText)findViewById(R.id.findemail_email);
        findemail_rand=(EditText)findViewById(R.id.findemail_rand);
        findemail_hqyzm=(TextView)findViewById(R.id.findemail_hqyzm);
        findemail_btn=(Button)findViewById(R.id.findemail_btn);
    }

    @Override
    void initEvent() {
        findemail_fh.setOnClickListener(this);
        findemail_hqyzm.setOnClickListener(this);
        findemail_btn.setOnClickListener(this);

    }

    @Override
    void initDate() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.findemail_fh:
                finish();
                break;
            case R.id.findemail_hqyzm:
                String email = findemail_email.getText().toString().trim();
                if (email.equals("")){
                    Toast.makeText(FindEmailPasswordActivity.this,"输入邮箱不能为空",Toast.LENGTH_SHORT).show();
                }
                else{
                    mDialog = WeiboDialogUtils.createLoadingDialog(FindEmailPasswordActivity.this, "获取验证码中...");
                    GetFindEmailRandModel getFindEmailRandModel = new GetFindEmailRandModel();
                    getFindEmailRandModel.GetFindEmailRand(email, new TListener<SuccessBean>() {
                        @Override
                        public void onResponse(SuccessBean successBean) {
                            if (successBean.getSuccess().equals("1")){
                                startTime();
                                Toast.makeText(FindEmailPasswordActivity.this,"获取验证码成功",Toast.LENGTH_SHORT).show();
                                DialogThridUtils.closeDialog(mDialog);
                            }
                            else if (successBean.getSuccess().equals("2")){
                                Toast.makeText(FindEmailPasswordActivity.this,"该邮箱没有注册",Toast.LENGTH_SHORT).show();
                                DialogThridUtils.closeDialog(mDialog);
                            }
                            else {
                                Toast.makeText(FindEmailPasswordActivity.this,"获取验证码失败",Toast.LENGTH_SHORT).show();
                                DialogThridUtils.closeDialog(mDialog);
                            }
                        }
                        @Override
                        public void onFail(String msg) {
                            Toast.makeText(FindEmailPasswordActivity.this,"获取验证码失败",Toast.LENGTH_SHORT).show();
                            DialogThridUtils.closeDialog(mDialog);
                        }
                    });
                }
                break;
            case R.id.findemail_btn:
                final String emails = findemail_email.getText().toString().trim();
                String rand = findemail_rand.getText().toString().trim();
                if (emails.equals("")||rand.equals("")){
                    Toast.makeText(FindEmailPasswordActivity.this,"邮箱或验证码不能为空",Toast.LENGTH_SHORT).show();
                }
                else {
                    GetFindEmailRandModel getFindEmailRandModel = new GetFindEmailRandModel();
                    getFindEmailRandModel.JudegFindEmailRand(emails, rand, new TListener<SuccessBean>() {
                        @Override
                        public void onResponse(SuccessBean successBean) {
                            if (successBean.getSuccess().equals("1")){
                                Toast.makeText(FindEmailPasswordActivity.this,"验证码成功",Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(FindEmailPasswordActivity.this,SetFindPasswordActivity.class);
                                intent.putExtra("email",emails);
                                intent.putExtra("setflag","2");
                                finish();
                                startActivity(intent);
                            }
                            else if (successBean.getSuccess().equals("2")){
                                Toast.makeText(FindEmailPasswordActivity.this,"验证码错误",Toast.LENGTH_SHORT).show();
                            }
                            else {
                                Toast.makeText(FindEmailPasswordActivity.this,"验证错误",Toast.LENGTH_SHORT).show();
                            }
                        }
                        @Override
                        public void onFail(String msg) {
                            Toast.makeText(FindEmailPasswordActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                break;
        }

    }
    private void startTime() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                if (i > 0) {
                    i--;
                    Message message = handler.obtainMessage();
                    message.arg1 = i;
                    handler.sendMessage(message);
                }
            }
        };
        timer.schedule(task, 1000);
    }
}
